package ca.etsmtl.log660.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnection {

	private static final String DB_SERVER	= "dijkstra.logti.etsmtl.ca";
	private static final String DB_NAME		= "LOG660";
	private static final String DB_PORT		= "1521";
	private static final String DB_USER		= "equipe8";
	private static final String DB_PASS		= "KsqPN7MW";
	private static final String URL			= "jdbc:oracle:thin:@//" + DB_SERVER + ":" + DB_PORT + "/" + DB_NAME;
	
	private static Connection connection = null;
	
	public static Connection getConnection() {
		if (connection == null) {
			try {
				DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
				Properties props = new Properties();
				props.put("user", DB_USER);
				props.put("password", DB_PASS);
				connection = DriverManager.getConnection(URL, props);
	
			} catch (Exception e) {
				System.out.println("A database exception has occured : " + e);
			}
		}

		return connection;
	}

	public static void closeConnection() {
		try {
			if (connection != null) {
				connection.commit();
				connection.close();
				connection = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
